package JFileChooserDemo;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class JFlieChooserDemo {
    private JLabel label = new JLabel("选择文件路径：");
    private JTextField jtf = new JTextField(25);
    private JButton button = new JButton("浏览");
    public JFlieChooserDemo()
    {
        JFrame jf = new JFrame("文件选择器");
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(jtf);
        panel.add(button);
        jf.add(panel);
        jf.setBounds(100,200,300,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser("D:\\");
                int val = fc.showOpenDialog(null);  //文件打开对话框
                if(val == fc.APPROVE_OPTION)  //证明打开了
                {
                    //正常选择文件
                    jtf.setText(fc.getSelectedFile().toString());
                }
                else
                {
                    //未正常选择文件，如选择取消按钮
                    jtf.setText("未选择文件");
                }
            }
        });
    }

    public static void main(String[] args) {
        new JFlieChooserDemo();
    }
}
